package com.hontech.bread.store

import com.hontech.bread.store.StoreAdv.Companion.TYPE_IMAGE
import com.hontech.bread.store.StoreAdv.Companion.TYPE_VIDEO
import com.hontech.bread.utils.runCPU
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.util.Calendar

@Serializable
class StoreAdvCfg(
    private val id: Long,

    @SerialName("adv_id")
    val advId: Long,

    val position: Int,

    @SerialName("play_start")
    val playStart: Int,

    @SerialName("play_end")
    val playEnd: Int,

    @SerialName("adv_type")
    val advType: Int,

    val name: String,

    val duration: Int,
) {

    companion object {

        const val CREATE_SQL = "CREATE TABLE IF NOT EXISTS tb_adv_cfg (" +
                "id INTEGER PRIMARY KEY, " +
                "adv_id INTEGER NOT NULL, " +
                "position INTEGER NOT NULL, " +
                "play_start INTEGER NOT NULL, " +
                "play_end INTEGER NOT NULL, " +
                "adv_type INTEGER NOT NULL, " +
                "name TEXT NOT NULL, " +
                "duration TEXT NOT NULL" +
                ")"

        suspend fun query() = runCPU {
            val cursor = Store.db.rawQuery(
                "SELECT " +
                        "id, adv_id, position, play_start, play_end, adv_type, name, duration " +
                        "FROM tb_adv_cfg", arrayOf()
            )
            cursor.use {
                val list = ArrayList<StoreAdvCfg>()
                while (cursor.moveToNext()) {
                    list.add(
                        StoreAdvCfg(
                            cursor.getLong(0),
                            cursor.getLong(1),
                            cursor.getInt(2),
                            cursor.getInt(3),
                            cursor.getInt(4),
                            cursor.getInt(5),
                            cursor.getString(6),
                            cursor.getInt(7)
                        )
                    )
                }
                list
            }
        }

        const val POSITION_TOP = 0
        const val POSITION_BOTTOM = 1
    }

    fun isAllowTime(): Boolean {
        val calendar = Calendar.getInstance()
        val h = calendar.get(Calendar.HOUR_OF_DAY)
        val m = calendar.get(Calendar.MINUTE)
        val s = calendar.get(Calendar.SECOND)
        val p = h * 3600 + m * 60 + s
        return (p >= playStart) && (p < playEnd)
    }

    suspend fun create() = runCPU {
        Store.db.execSQL(
            "INSERT INTO " +
                    "tb_adv_cfg (id, adv_id, position, play_start, play_end, adv_type, name, duration) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
            arrayOf(id, advId, position, playStart, playEnd, advType, name, duration)
        )
    }

    suspend fun delete() = runCPU {
        Store.db.execSQL("DELETE FROM tb_adv_cfg WHERE id = ?", arrayOf(id))
    }

    fun isImage() = advType == TYPE_IMAGE
    fun isVideo() = advType == TYPE_VIDEO

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as StoreAdvCfg

        return id == other.id
    }

    override fun hashCode(): Int {
        return id.hashCode()
    }


}

















